// 权限树工具：叶子/全部ID与搜索过滤

export const getLeafIds = (nodes: any[]): string[] => {
  const ids: string[] = [];
  const walk = (arr: any[]) =>
    (arr || []).forEach((n) => {
      if (n?.children?.length) walk(n.children);
      else ids.push(String(n.id));
    });
  walk(nodes || []);
  return ids;
};

export const getAllIds = (nodes: any[]): string[] => {
  const ids: string[] = [];
  const walk = (arr: any[]) =>
    (arr || []).forEach((n) => {
      ids.push(String(n.id));
      if (n?.children?.length) walk(n.children);
    });
  walk(nodes || []);
  return ids;
};

export const filterTreeBySearch = (nodes: any[], keyword: string): any[] => {
  if (!keyword) return nodes;
  const lower = keyword.toLowerCase();
  const walk = (arr: any[]): any[] => {
    const res: any[] = [];
    (arr || []).forEach((n) => {
      const label = String(n.name || n.label || '').toLowerCase();
      const ok = label.includes(lower);
      const children = n.children ? walk(n.children) : [];
      if (ok || children.length) {
        res.push({ ...n, children });
      }
    });
    return res;
  };
  return walk(nodes || []);
};


